﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace grounding.parser.Parsers
{
    public class DemoActionParser : IParser
    {
        private Dictionary<string, DemoActionTemplate> templateSelector = new Dictionary<string, DemoActionTemplate>();

        public DemoActionParser()
        {
            DemoActionTemplate walking = DemoActionTemplate.Walking;
            //DemoActionTemplate waving = DemoActionTemplate.Waving;
            DemoActionTemplate raiseArms = DemoActionTemplate.RaiseArms;
            DemoActionTemplate openHands = DemoActionTemplate.OpenHands;
            DemoActionTemplate closeHands = DemoActionTemplate.CloseHands;
            DemoActionTemplate  unstiff = DemoActionTemplate.Unstiff;
            DemoActionTemplate  stiff = DemoActionTemplate.Stiff;
            templateSelector.Add("come", walking);
            templateSelector.Add("go", walking);
            templateSelector.Add("walk", walking);
            templateSelector.Add("move", walking);
            templateSelector.Add("jog", walking);
            templateSelector.Add("sprint" , walking);
            templateSelector.Add("run", walking);
            templateSelector.Add("towards", walking);
            templateSelector.Add("pace", walking);
            templateSelector.Add("locomote", walking);
            templateSelector.Add("shamble", walking);
            templateSelector.Add("forward", walking);
            templateSelector.Add("backward", walking);
            templateSelector.Add("back", walking);
            templateSelector.Add("feet", walking);

            /*
            templateSelector.Add("arm", waving);
            templateSelector.Add("wave", waving);
            templateSelector.Add("forearm", waving);
            templateSelector.Add("hand", waving);
            templateSelector.Add("hello", waving);
            templateSelector.Add("hi", waving);
            templateSelector.Add("goodbye", waving);
            templateSelector.Add("waving", waving);
            */

            templateSelector.Add("arms", raiseArms);
            templateSelector.Add("raise", raiseArms);
            templateSelector.Add("lift", raiseArms);
            templateSelector.Add("hold", raiseArms);
            templateSelector.Add("carry", raiseArms);
            templateSelector.Add("arm", raiseArms);

            templateSelector.Add("open", openHands);
            templateSelector.Add("grip", openHands);
            templateSelector.Add("grasp", openHands);

            templateSelector.Add("close", closeHands);
            templateSelector.Add("release", closeHands);
            templateSelector.Add("let", closeHands);

            templateSelector.Add("unstiff", unstiff);
            templateSelector.Add("relax", unstiff);

            templateSelector.Add("stiff", stiff);

        }

        #region IParser Members

        public ILogicElement Parse(string naturalText)
        {
            DemoActionTemplate selected = null;
            foreach (string identifier in templateSelector.Keys)
            {
                // Doesn't deal with overlap in template selection
                if (naturalText.Contains(identifier))
                {
                    if (templateSelector[identifier].Type == "walking")
                        selected = DemoActionTemplate.Walking;
                    else if (templateSelector[identifier].Type == "raiseArms")
                        selected = DemoActionTemplate.RaiseArms;
                    else if (templateSelector[identifier].Type == "openHands")
                        selected = DemoActionTemplate.OpenHands;
                    else if (templateSelector[identifier].Type == "closeHands")
                        selected = DemoActionTemplate.CloseHands;
                    else if (templateSelector[identifier].Type == "stiff")
                        selected = DemoActionTemplate.Stiff;
                    else if (templateSelector[identifier].Type == "unstiff")
                        selected = DemoActionTemplate.Unstiff;

                    break;
                }
            }

            // Didn't find a template that matches
            if (selected == null)
            {
                Console.WriteLine("Found no matching template!");
                return null;
            }


            //selected.Clear();
            selected.FillTemplate(naturalText);

            return selected;
        }

        #endregion
    }
}
